using UnityEngine;
using EchoFramework;
using UnityEngine.UI;

/// <summary>
/// 选择关卡界面
/// </summary>
public class SelectLevelView : UIViewBase
{
    private EchoButton exitBtn;
    private GridLayoutGroup gridLayout;

    public override void OnAwake()
    {
        base.OnAwake();
        exitBtn = prefabBinder.Find("ExitBtn").GetComponent<EchoButton>();
        exitBtn.onClick.AddListener(OnClickExitBtn);
        gridLayout = prefabBinder.Find("GridLayout").GetComponent<GridLayoutGroup>();
    }

    public override void OnStart(params object[] objs)
    {
        base.OnStart(objs);
        InitLevelItem();
    }

    /// <summary>
    /// 点击退出按钮 返回首页
    /// </summary>
    private void OnClickExitBtn()
    {
        UIViewManager.Instance.OpenWindow(UIViewID.MainMenuView);
        CloseWindow();
    }
    
    /// <summary>
    /// 生成关卡Item
    /// </summary>
    private void InitLevelItem()
    {
        int maxLevel = MapLogicModule.Instance.MaxLevel;
        for (int i = 0; i < maxLevel; i++)
        {
            LevelItem item = AssetManager.Instance.LoadGameObject<LevelItem>(
                    "Assets/GameRes/UIPrefab/SelectLevelView/LevelItem.prefab", gridLayout.transform);
            item.SetData(new LevelItemData(i + 1));
        }
    }
}